package controller;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import javax.swing.ComboBoxModel;
import javax.swing.JOptionPane;

import model.MyEllipse;
import model.MyImage;
import model.MyShape;
import view.AddShapeOne;
import view.AddShapeThree;
import view.AddShapeTwo;
import view.ViewShapes;
import view.interfaces.IHome;

public class ControllerHome implements ActionListener {

    private IHome view;

    private ControllerAddShapeThree controllerAddShapeThree;
    private ControllerAddShapeTwo controllerAddShapeTwo;
    private ControllerAddShapeOne controllerAddShapeOne;
    private ControllerViewShapes controllerViewShapes;
    private ControllerEditShape controllerEditShape;
    
    public static MyImage image = new MyImage();
    public static String[] shapesAvailable = { "Rectangle", "Ellipse" };

	public static Color[] colorsAvailable = { Color.black,Color.blue,Color.cyan, Color.darkGray, Color.gray, Color.green,
		Color.lightGray,Color.magenta,Color.orange, Color.pink, Color.red, Color.white, Color.yellow };

    public ControllerHome() {

    	controllerAddShapeOne = new ControllerAddShapeOne();
    	controllerAddShapeTwo = new ControllerAddShapeTwo();
    	controllerAddShapeThree = new ControllerAddShapeThree();
    	controllerViewShapes = new ControllerViewShapes();
    	controllerEditShape = new ControllerEditShape();
            
    }

    public void setView(IHome viewEntry) {
            
            this.view = viewEntry;
            
    }

    public IHome getView() {
            
            return view;
            
    }
    
    /*
     * 
     * Liste des get controlleurs
     */
    public ControllerAddShapeOne getControllerAddShapeOne() {

            return this.controllerAddShapeOne;
            
    }
    
    public ControllerAddShapeTwo getControllerAddShapeTwo() {

        return this.controllerAddShapeTwo;
        
    }
    
    public ControllerAddShapeThree getControllerAddShapeThree() {

        return this.controllerAddShapeThree;
        
    }
    
    public ControllerViewShapes getControllerViewShapes() {

        return this.controllerViewShapes;
        
    }
    
    public ControllerEditShape getControllerEditShape() {

        return this.controllerEditShape;
        
    }


    public void actionPerformed(ActionEvent e) {
    	
    	String action = e.getActionCommand();

    	if(action.startsWith("ADDSHAPEONE")) {
    		
    		new AddShapeOne(controllerAddShapeOne);
    		
    	}
    	else if(action.startsWith("ADDSHAPETWO")) {
		    		
    		new AddShapeTwo(controllerAddShapeTwo);
    		
    	}
    	else if(action.startsWith("ADDSHAPETHREE")) {
			
			new AddShapeThree(controllerAddShapeThree);
			
		}
    	else if(action.startsWith("NEWFILE")) {
    		
    		this.image.reset();
    		
    	}
    	
    	else if(action.startsWith("SAVE")) {
    		
    		try {
    			
				ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("file.ser"));
				
				for(int i = 0; i < this.image.getNumberShapes(); i++) {
					
					oos.writeObject(this.image.getShape(i));
					
				}
				
				oos.flush();
				
			} catch (Exception ee) {
				
				ee.printStackTrace();
				
			}
    		
    	}
    	
    	else if(action.startsWith("OPEN")) {
    		
    		try {
    			
				ObjectInputStream ois = new ObjectInputStream(new FileInputStream("file.ser"));
				
				ois.close();

				
			} catch (Exception ee) {
				
				ee.printStackTrace();
				
			}
    		
    	}

    }

}